﻿using System;
using RayDen.Library.Components.SystemComponents;
using RayDen.RayEngine.Core.Types;

namespace RayDen.RayEngine.Engines.ProgressivePhotonMapping
{
    public class PPMPathBuffer : MultiPathBufferBase
    {
        internal PPMContext context;
        public PPMPathBuffer(PPMContext ctx)
        {
            context = ctx;
        }

        protected override PathSamplerBase CreatePathSampler()
        {
            switch (GetCurrentMode())
            {
                case 0:
                    return new PPMEyePath { SurfaceSampler = surfaceSampler, pathIntegrator = this };
                case 1:
                    return new PPMLightPath { SurfaceSampler = surfaceSampler, pathIntegrator = this };
            }
            return null;
        }

        protected override int GetCurrentMode()
        {
            if (!context.HitPointsGathered)
            {
                modeSwitched = false;
                return 0;
            }

            return 1;
        }

        private bool modeSwitched = false;

        internal override bool IsModeSwitch()
        {
            if (modeSwitched)
                return context.Splat;
            return context.HitPointsGathered;
        }

        internal override void Splat(SampleBuffer buffer)
        {
            //base.Splat(buffer);
        }

        internal override void OnModeSwitch()
        {
            Tracer.TraceLine("{0} Photons gathered, {1} Total photons used", context.photons, context.TotalPhotons);
            //    context.ResetPhotons();
            context.BuildGrid();
            modeSwitched = !modeSwitched;
            paths[0].Clear();
            paths[1].Clear();
        }
    }
}